package com.application.chiettinhdienluc.ui;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.application.chiettinhdienluc.R;
import com.application.chiettinhdienluc.app.MainApplication;
import com.application.chiettinhdienluc.db.NhanVienDataSource;
import com.application.chiettinhdienluc.model.NguoiDungModel;
import com.application.chiettinhdienluc.utils.Utils;


public class AccountSettingActivity extends SherlockFragmentActivity {

	final Context context = this;
	private Resources res;
	public static final String BOOL_EXTRA_START_FROM_LOGIN = "extra_start_from_login";

	private EditText tv_username_value;
	private EditText tv_password_value;
	private EditText tv_new_password_value;
	private EditText tv_password_reinput_value;
	private EditText tv_display_name_value;

	private NguoiDungModel nd = null;
	private boolean isAdmin = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.account_settings_view);

		res = getResources();
		getSupportActionBar().setTitle(res.getString(R.string.str_title_account_settings));

		tv_username_value = (EditText) findViewById(R.id.tv_username_value);
		tv_password_value = (EditText) findViewById(R.id.tv_password_value);
		tv_new_password_value = (EditText) findViewById(R.id.tv_new_password_value);
		tv_password_reinput_value = (EditText) findViewById(R.id.tv_password_reinput_value);
		tv_display_name_value = (EditText) findViewById(R.id.tv_display_name_value);

		ActionBar actionbar = getSupportActionBar();
		if(actionbar != null)
		{
			actionbar.setDisplayHomeAsUpEnabled(true);
			actionbar.setDisplayShowHomeEnabled(true);
		}

		nd = MainApplication.getNguoiDung();

		if(nd != null)
		{
			isAdmin = nd.get_PhanQuyen().equals("admin");
			
			tv_display_name_value.setText(nd.get_TenNguoiDung());
			tv_username_value.setText(nd.get_TenDangNhap());
			tv_username_value.setEnabled(false);
			
			tv_password_value.setText("");
			tv_new_password_value.setText("");
			tv_password_reinput_value.setText("");

			tv_display_name_value.setEnabled(isAdmin);
		}
	}

	// 1: update dpn
	// 2: update pass
	// 3: update all
	private void SaveAccountSettings(String username, String dpn, String pass, String new_pass, boolean isAdmin, int caseUpdate) {
		int numRowHasEffect = 0;
		NhanVienDataSource ndDataSource = new NhanVienDataSource();
		numRowHasEffect = ndDataSource.update(username, isAdmin ? dpn : "", new_pass, caseUpdate);

		String noticeString = "";
		
		if(numRowHasEffect > 0)
		{
			if(caseUpdate == 1)
			{
				MainApplication.getNguoiDung().set_TenNguoiDung(dpn);
				noticeString = res.getString(R.string.str_change_dpn_success);
			}	
			else if(caseUpdate == 2)
			{
				MainApplication.getNguoiDung().set_MatKhau(new_pass);
				noticeString = res.getString(R.string.str_change_password_success);
			}	
			else if(caseUpdate == 3)
			{
				MainApplication.getNguoiDung().set_TenNguoiDung(dpn);
				MainApplication.getNguoiDung().set_MatKhau(new_pass);
				
				noticeString = res.getString(R.string.str_change_all_success);
			}
		}
		else
			noticeString = res.getString(R.string.str_change_password_fail);
			
		Utils.showMess(noticeString);
	}

	private int checkCanSave(String username, String dpn, String pass, String new_pass, String passReInput)
	{
		int result = 0;
		
		if(isAdmin)
		{
			if(TextUtils.isEmpty(pass))
			{
				if(TextUtils.isEmpty(dpn))
				{
					Utils.showMess(res.getString(R.string.str_pls_input_field_to_update));
					return 0;
				}	
				else
				{
					if(!dpn.equals(nd.get_TenNguoiDung()))
					{
						return 1;
					}
					else
					{
						Utils.showMess(res.getString(R.string.str_pls_input_field_to_update));
						return 0;
					}
				}
			}
			else
			{
				if(TextUtils.isEmpty(dpn) || dpn.equals(nd.get_TenNguoiDung()))
				{
					if(!pass.equals(nd.get_MatKhau()))
					{
						Utils.showMess(res.getString(R.string.str_notice_pass_invalid));
						return 0;
					}
					else
					{
						if(TextUtils.isEmpty(new_pass))
						{
							Utils.showMess(res.getString(R.string.str_notice_input_new_pass));
							return 0;
						}
						else
						{
							if(!new_pass.equals(passReInput))
							{
								Utils.showMess(res.getString(R.string.str_error_pass_reinput_invalid));
								return 0;
							}
							else
							{
								return 2;
							}
						}
					}
				}
				else
				{
					if(!pass.equals(nd.get_MatKhau()))
					{
						Utils.showMess(res.getString(R.string.str_notice_pass_invalid));
						return 0;
					}
					else
					{
						if(TextUtils.isEmpty(new_pass))
						{
							Utils.showMess(res.getString(R.string.str_notice_input_new_pass));
							return 0;
						}
						else
						{
							if(!new_pass.equals(passReInput))
							{
								Utils.showMess(res.getString(R.string.str_error_pass_reinput_invalid));
								return 0;
							}
							else
							{
								return 3;
							}
						}
					}
				}
				
			}
		}
		else
		{
			if(TextUtils.isEmpty(pass))
			{
				Utils.showMess(res.getString(R.string.str_pls_input_field_to_update));
				return 0;
			}
			else
			{
				if(!pass.equals(nd.get_MatKhau()))
				{
					Utils.showMess(res.getString(R.string.str_notice_pass_invalid));
					return 0;
				}
				else
				{
					if(TextUtils.isEmpty(new_pass))
					{
						Utils.showMess(res.getString(R.string.str_notice_input_new_pass));
						return 0;
					}
					else
					{
						if(!new_pass.equals(passReInput))
						{
							Utils.showMess(res.getString(R.string.str_error_pass_reinput_invalid));
							return 0;
						}
						else
						{
							return 2;
						}
					}
				}
			}
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId())
		{
		case android.R.id.home:
			finish();
			break;

		case ID_ACTION_SAVE:
			SaveAccountSettings(tv_username_value.getText().toString(), tv_display_name_value.getText().toString(), tv_password_value.getText().toString(), tv_new_password_value.getText().toString(), nd.get_PhanQuyen().equals("admin"), checkCanSave(tv_username_value.getText().toString(), tv_display_name_value.getText().toString(), tv_password_value.getText().toString(), tv_new_password_value.getText().toString(), tv_password_reinput_value.getText().toString()));
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		finish();
	}

	private final int ID_ACTION_SAVE = 1;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, ID_ACTION_SAVE, Menu.NONE, "Save").setIcon(R.drawable.content_save).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		return true;
	}

}
